package com.tianyu.sports.controller;

import com.tianyu.sports.bean.Result;
import com.tianyu.sports.exception.VipException;
import com.tianyu.sports.po.VipinfoHistory;
import com.tianyu.sports.service.impl.VipService;
import com.tianyu.sports.utils.ResultUtil;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @author Jzen
 * @Title: sports
 * @Package com.tianyu.sports.controller
 * @Description: ${todo}
 * @date 2018/5/28 10:24
 */
@RestController
//@CrossOrigin(origins = "http://47.106.218.128.877",maxAge = 3600)
public class VipController {

    @Autowired
    private VipService vipService;

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/getSmsInfo",method = RequestMethod.POST)
    public Result getSmsInfo(String phone){
        try{
            return ResultUtil.success(vipService.getSmsInfo(phone));
        }catch (VipException e){
            return ResultUtil.error(e.getCode(),e.getMessage());
        }
    }
    /**
     * 注册会员信息
     *
     * @param vipinfoHistory
     * @return
     */
    @RequestMapping(value = "/putMembers",method = RequestMethod.POST)
    public Result putMembers(VipinfoHistory vipinfoHistory){
        try{
            return ResultUtil.success(vipService.putMembers(vipinfoHistory));
        }catch (VipException e){
            return ResultUtil.error(e.getCode(),e.getMessage());
        }
    }
//    /**
//     * 注册会员信息
//     *
//     * @param vipinfo
//     * @return
//     */
//    @RequestMapping(value = "/putMembers",method = RequestMethod.POST)
//    public Result putMembers(Vipinfo vipinfo){
//        try{
//            return ResultUtil.success(vipService.putMembers(vipinfo));
//        }catch (VipException e){
//            return ResultUtil.error(e.getCode(),e.getMessage());
//        }
//    }
    /**
     * 检查电话号
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/checkPhone",method = RequestMethod.GET)
    public Result checkPhone(String phone){
        try{
            vipService.checkPhone(phone);
            return ResultUtil.success();
        }catch (VipException e){
            return ResultUtil.error();
        }catch (BindingException e){
            return ResultUtil.success();
        }
    }
    /**
     * 检查身份证号
     *
     * @param IDNumber
     * @return
     */
    @RequestMapping(value = "/checkIDNumber",method = RequestMethod.GET)
    public Result checkIDNumber(String IDNumber){
        try{
            vipService.checkIDNumber(IDNumber);
            return ResultUtil.success();
        }catch (VipException e){
            return ResultUtil.error();
        }catch (BindingException e){
            return ResultUtil.success();
        }
    }
    /**
     * 注册账号信息反馈
     * @param id
     * @param ordernumber
     * @return
     */
    @RequestMapping(value = "/registerSuccess",method = RequestMethod.GET)
    public Result registerSuccess(Integer id,String ordernumber){
        try{
            return ResultUtil.success(vipService.registerSuccess(id,ordernumber));
        }catch (VipException e){
            return ResultUtil.error(e.getCode(),e.getMessage());
        }
    }
    /**
     * 微信支付
     *
     * @param totalfee
     * @param userip
     * @return
     */
    @RequestMapping(value = "/weixinPay",method = RequestMethod.POST)
    public Result weixinPay(String totalfee,String userip){
        try{
            System.out.println(totalfee);
            Map<String, String> map = vipService.weixinPay(totalfee,userip);
            if(map.get("return_code").equals("FAIL")){
                return ResultUtil.error(-1,map.get("return_msg"));
            }
            return ResultUtil.success(map);
        }catch (VipException e){
            return ResultUtil.error(e.getCode(),e.getMessage());
        }
    }
    /**
     * 注册账号信息反馈
     * @param id
     * @return
     */
    @RequestMapping(value = "/registerSuccessByGZH",method = RequestMethod.GET)
    public Result registerSuccessByGZH(Integer id){
        try{
            return ResultUtil.success(vipService.registerSuccessByGZH(id));
        }catch (VipException e){
            return ResultUtil.error(e.getCode(),e.getMessage());
        }
    }
    /**
     * 微信公众号支付
     * @param totalfee
     * @param userip
     * @param openid
     * @return
     */
    @RequestMapping(value = "/weixinPublicPay",method = RequestMethod.POST)
    public Result weixinPublicPay(String totalfee,String userip,String openid){
        Map<String,String> map = vipService.weixinPublicPay(totalfee,userip,openid);
        if(map.get("return_code").equals("FAIL")) {
            return ResultUtil.error(-1,map.get("return_msg"));
        }else{
            return ResultUtil.success(map);
        }
    }
    /**
     * 回调接口
     *
     */
    @RequestMapping("/notityinfo")
    public void notityinfo(){

    }
}
